package com.cqut.hfh.ordering.controller;

import com.cqut.hfh.ordering.pojo.EhyRole;
import com.cqut.hfh.ordering.pojo.FodFood;
import com.cqut.hfh.ordering.pojo.FoodType;
import com.cqut.hfh.ordering.service.IFoodTypeService;
import com.cqut.hfh.ordering.utils.EntityIdutils;
import com.cqut.hfh.ordering.utils.Message;
import org.aspectj.bridge.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * create by  admin on 2019/6/25
 */
@Controller
@RequestMapping("/foodType")
public class FoodTypeController {
    @Autowired
    private IFoodTypeService foodTypeService;

    /**
     * 显示食品类型列表
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping(path="/findAlls",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String getAllFoodType(@RequestParam("limit") String limit,@RequestParam("page") String page){
        List<Map<String, Object>> foodType_list = foodTypeService.getAllFoodType(limit, page);
        init(foodType_list);
        Integer count = foodTypeService.getCount();
        return Message.getReturnMsg(0,"", count, foodType_list);
    }
    @RequestMapping(path="/findAllType",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String getAllType(){
        List<FoodType> Type_list = foodTypeService.getAllType();
        Integer count = foodTypeService.getCount();
        return Message.getReturnMsg(0,"", count, Type_list);
    }

    /**
     * 添加食品类型
     * @param foodType
     * @return
     */
    @RequestMapping(path = "/foodTypeAdd",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String foodTypeAdd(FoodType foodType){
        foodTypeService.AddType(foodType);
        return Message.getReturnMsg(0,"");
    }

    @RequestMapping(path = "/foodTypeUpdate",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String TypeUpdate(FoodType foodType){
        foodTypeService.updateType(foodType);
        return Message.getReturnMsg(0,"");
    }


    @RequestMapping(path = "/findTypeByName",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findTypeByName(@RequestParam("limit") String limit,@RequestParam("page") String page,
                                 @RequestParam("type_name") String type_name){
        List<Map<String, Object>> type_list = foodTypeService.getTypeByName(limit, page, type_name);
        init(type_list);
        Integer count = foodTypeService.getCount();
        return Message.getReturnMsg(0,"", count, type_list);

    }
    @RequestMapping(path = "/findById")
    @ResponseBody
    public FoodType findById(@RequestParam("id") String type_id){
        Map<String, Object> map = foodTypeService.findById(type_id);
        FoodType foodType = new FoodType();
        foodType.setProperties(map);
        return foodType;
    }

    @RequestMapping(path = "/foodTypeDelete",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String deleteById(@RequestParam("id") String type_id){

        foodTypeService.delById(type_id);
        return Message.getReturnMsg(0,"ok");

    }

    public void init(List<Map<String, Object>> list){
        for( Map<String, Object> map : list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }
    }


}
